home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 1
/
SPACE - Library 1 - Volume 1.iso
/
program
/
52
/
quiksort
/
quiksort.lst
< prev
Wrap
File List
|
1987-04-02
|
3KB
|
126 lines
'
' Arrays To Be Dimensioned For QuickSort
'
Dim A$(Xq%) ! String Array To Be Sorted
Dim Sort$(Xq%) ! Temporary String Array used By QuickSort
Dim Sort%(Xq%) ! Index Array
Dim Pq%(20),Wq%(20) ! Arrays Used By QuickSort
'
' Copy String Array to be Sorted in Temporary Array
'
For I%=0 To Xq% ! Xq% = Number Of Elements In Array
Sort%(I%)=I% ! Initialize Index Array
Sort$(I%)=A$(I%) ! Sort$() = Tempoary Sort String
' ! A$() = String Array To Be Sorted
Next I%
'
' Initialize Quick Sort Variables
'
Kq%=1
Pq%(Kq%)=0 ! Start Count Of Array
Wq%(Kq%)=Xq% ! End Count Of Array
Dq%=0 ! Start Count Of Array
Rq%=Xq% ! End Count Of Array
'
' ---------------------------- QUICK SORT ALGORITHM ----------------------------
Point_1:
If Rq%-Dq%<9
Goto Point_10
Endif
Iq%=Dq%
Jq%=Rq%
' ------------------------------------------------------------------------------
Point_2:
If Sort$(Iq%)>Sort$(Jq%)
Goto Point_5
Endif
' ------------------------------------------------------------------------------
Point_3:
Dec Jq%
If Jq%>Iq%
Goto Point_2
Endif
Inc Jq%
' ------------------------------------------------------------------------------
Point_4:
Inc Kq%
If (Iq%-Dq%)<(Rq%-Jq%)
Goto Point_9
Endif
Pq%(Kq%)=Dq%
Wq%(Kq%)=Iq%
Dq%=Jq%
Goto Point_1
' ------------------------------------------------------------------------------
Point_5:
Tq$=Sort$(Jq%)
Tq%=Sort%(Jq%)
Sort$(Jq%)=Sort$(Iq%)
Sort%(Jq%)=Sort%(Iq%)
Sort$(Iq%)=Tq$
Sort%(Iq%)=Tq%
Goto Point_7
' ------------------------------------------------------------------------------
Point_6:
If Sort$(Jq%)<Sort$(Iq%)
Goto Point_8
Endif
' ------------------------------------------------------------------------------
Point_7:
Inc Iq%
If Jq%>Iq%
Goto Point_6
Endif
Inc Jq%
Goto Point_4
' ------------------------------------------------------------------------------
Point_8:
Tq$=Sort$(Jq%)
Tq%=Sort%(Jq%)
Sort$(Jq%)=Sort$(Iq%)
Sort%(Jq%)=Sort%(Iq%)
Sort$(Iq%)=Tq$
Sort%(Iq%)=Tq%
Goto Point_3
' ------------------------------------------------------------------------------
Point_9:
Pq%(Kq%)=Jq%
Wq%(Kq%)=Rq%
Rq%=Iq%
Goto Point_1
' ------------------------------------------------------------------------------
Point_10:
If (Rq%-Dq%+1)=1
Goto Point_11
Endif
For Iq%=Dq%+1 To Rq%
For Jq%=Dq% To Iq%-1
Cq%=Iq%-Jq%+Dq%-1
If Sort$(Cq%)<=Sort$(Cq%+1)
Jq%=Iq%-1
Goto Drop_out
Endif
Tq$=Sort$(Cq%)
Tq%=Sort%(Cq%)
Sort$(Cq%)=Sort$(Cq%+1)
Sort%(Cq%)=Sort%(Cq%+1)
Sort$(Cq%+1)=Tq$
Sort%(Cq%+1)=Tq%
Drop_out:
Next Jq%
Next Iq%
' ------------------------------------------------------------------------------
Point_11:
Dq%=Pq%(Kq%)
Rq%=Wq%(Kq%)
Dec Kq%
If Kq%=0
Goto Sort_done
Endif
Goto Point_1
' ------------------------------------------------------------------------------
Sort_done:
'
' Sort%() Contains an Index to A$()
'